package com.zhengyisky.mapper;

import com.zhengyisky.pojo.*;
import org.apache.ibatis.annotations.*;

import java.util.Date;
import java.util.List;

@Mapper
public interface PurchaseMapper {
    @Select("select * from tb_company")
    List<Company> findAllCompany();
    @Select("select * from tb_project")
    List<Project> findAllProject();
    @Select("select * from tb_goods")
    List<Goods> findAllGoods();
    @Select("select * from tb_type")
    List<Type> findAllType();
    @Insert("insert into tb_order set order_code=#{orderCode}," +
            "name=#{name},type_id=#{typeId},content=#{content}," +
            "project_id=#{projectId},company_id=#{companyId}," +
            "sum_price=#{sumPrice},money_type=#{moneyType},pre_price=#{prePrice}," +
            "start_date=#{startDate},end_date=#{endDate},sign_date=#{signDate}," +
            "tax_type=#{taxType},tax_value=#{taxValue},user_id=#{userId}," +
            "file_path=#{filePath},status=#{status},create_time=now()")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void saveOrder(Order order);

    @Insert("insert into tb_order_detail set order_id=#{orderId},goods_id=#{goodsId},price=#{price}," +
            "count=#{count},unit=#{unit},subtotal_price=#{subtotalPrice},content=#{content}")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void saveOrderDetail(OrderDetail orderDetail);

    @Select("select a.id,a.order_code, a.name,b.name type_name,c.name project_name," +
            "d.name company_name,a.sign_date,e.username,f.dept_name,a.create_time,a.status from tb_order a " +
            "left join tb_type b on a.type_id=b.id " +
            "left join tb_project c on a.project_id=c.id " +
            "left join tb_company d on a.company_id=d.id " +
            "left join tb_user e on a.user_id=e.id " +
            "left join tb_dept f on e.dept_id=f.id " +
            "where a.status = 2")
    List<Order> list();

    @Select("select a.id,a.order_code, a.name,b.name type_name,c.name project_name," +
            "d.name company_name,a.sign_date,e.username,f.dept_name,a.create_time,a.status from tb_order a " +
            "left join tb_type b on a.type_id=b.id " +
            "left join tb_project c on a.project_id=c.id " +
            "left join tb_company d on a.company_id=d.id " +
            "left join tb_user e on a.user_id=e.id " +
            "left join tb_dept f on e.dept_id=f.id where a.user_id=#{id}")
    List<Order> listByUser(Integer id);

    @Select("select a.id,a.order_code, a.name,b.name type_name,c.name project_name," +
            "d.name company_name,a.sign_date,e.username,f.dept_name,a.create_time,a.status from tb_order a " +
            "left join tb_type b on a.type_id=b.id " +
            "left join tb_project c on a.project_id=c.id " +
            "left join tb_company d on a.company_id=d.id " +
            "left join tb_user e on a.user_id=e.id " +
            "left join tb_dept f on e.dept_id=f.id where a.status!=0")
    List<Order> listByStatus();

    @Update("update tb_order set status=1 where id=#{id}")
    void submitApprove(Integer id);

    @Select("select a.*,b.username from tb_order a left join tb_user b on a.user_id=b.id where a.id=#{id}")
    Order findOrderById(Integer id);

    @Select("select * from tb_order_detail where order_id=#{id}")
    List<OrderDetail> findOrderDetailById(Integer id);

    @Update("update tb_order set order_code=#{orderCode}," +
            "name=#{name},type_id=#{typeId},content=#{content}," +
            "project_id=#{projectId},company_id=#{companyId}," +
            "sum_price=#{sumPrice},money_type=#{moneyType},pre_price=#{prePrice}," +
            "start_date=#{startDate},end_date=#{endDate},sign_date=#{signDate}," +
            "tax_type=#{taxType},tax_value=#{taxValue},user_id=#{userId}," +
            "file_path=#{filePath},status=#{status} where id=#{id}")
    void updateOrder(Order order);

    @Update("update tb_order_detail set order_id=#{orderId},goods_id=#{goodsId},price=#{price}," +
            "count=#{count},unit=#{unit},subtotal_price=#{subtotalPrice},content=#{content} where id=#{id}")
    void updateOrderDetail(OrderDetail orderDetail);

    void deleteOrderDetail(OrderEntity orderEntity);

    @Update("update tb_order set status=#{status},create_time=now() where id=#{id}")
    void approve(Integer id, Integer status);


    @Select("select a.id,a.order_code, a.name,b.name type_name,c.name project_name," +
            "d.name company_name,a.sign_date,e.username,f.dept_name,a.create_time,a.status from tb_order a " +
            "left join tb_type b on a.type_id=b.id " +
            "left join tb_project c on a.project_id=c.id " +
            "left join tb_company d on a.company_id=d.id " +
            "left join tb_user e on a.user_id=e.id " +
            "left join tb_dept f on e.dept_id=f.id " +
            "where a.create_time>=#{lastTime} and a.status=2")
    List<Order> listByTime(Date lastTime);

    @Select("select * from tb_project where id=#{projectId}")
    Project findProjectById(Integer projectId);

    @Update("update tb_project set total_money=#{newTotalMoney} where id=#{projectId}")
    void updateProject(Integer projectId, Integer newTotalMoney);
}
